package com.ztewatch.wearcontacts.calllog;

import android.content.Context;
import android.database.Cursor;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.common.widget.GroupingListAdapter;
import com.ztewatch.wearcontacts.R;

public class RejectedCallLogAdapter extends GroupingListAdapter implements
		CallLogGroupBuilder.GroupCreator {
	private static final int MAX_CALL_TYPE_ICONS = 3;
	CallLogGroupBuilder mCallLogGroupBuilder;
	LayoutInflater mInflater;
	Context mContext;

	public RejectedCallLogAdapter(Context context) {
		super(context);
		mContext = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mCallLogGroupBuilder = new CallLogGroupBuilder(this);
	}

	public void bindView(View view, Cursor c, final int count) {
		final String number = c.getString(CallLogQuery.NUMBER);
		final long date = c.getLong(CallLogQuery.DATE);

		TextView number_textview = (TextView) view
				.findViewById(R.id.rejectedcall_log_number);
		TextView date_textview = (TextView) view
				.findViewById(R.id.rejectedcall_log_date);

		number_textview.setText(number);
		final Integer callCount;
		if (count > MAX_CALL_TYPE_ICONS) {
			callCount = count;
		} else {
			callCount = null;
		}
		CharSequence dateText = getCallDate(date);
		setCallCountAndDate(date_textview, callCount, dateText);

	}

	private void setCallCountAndDate(TextView views, Integer callCount,
			CharSequence dateText) {
		// Combine the count (if present) and the date.
		final CharSequence text;
		if (callCount != null) {
			text = mContext.getResources().getString(
					R.string.call_log_item_count_and_date,
					callCount.intValue(), dateText);
		} else {
			text = dateText;
		}

		views.setText(text);
	}

	private CharSequence getCallDate(long date) {
		return DateUtils.getRelativeTimeSpanString(date,
				System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS,
				DateUtils.FORMAT_ABBREV_RELATIVE);
	}

	@Override
	protected void addGroups(Cursor cursor) {
		mCallLogGroupBuilder.addGroups(cursor);
	}

	@Override
	public void addGroup(int cursorPosition, int size, boolean expanded) {
		super.addGroup(cursorPosition, size, expanded);
	}

	@Override
	public void setDayGroup(long rowId, int dayGroup) {
	}

	@Override
	public void clearDayGroups() {
	}

	@Override
	protected void bindChildView(View view, Context arg1, Cursor cursor) {
		bindView(view, cursor, 1);
	}

	@Override
	protected void bindGroupView(View view, Context arg1, Cursor cursor,
			int groupSize, boolean arg4) {
		bindView(view, cursor, groupSize);
	}

	@Override
	protected void bindStandAloneView(View view, Context arg1, Cursor cursor) {
		bindView(view, cursor, 1);
	}

	@Override
	protected View newChildView(Context arg0, ViewGroup parent) {
		return mInflater.inflate(R.layout.rejected_call_log_list_view, parent,
				false);
	}

	@Override
	protected View newGroupView(Context context, ViewGroup parent) {
		return newChildView(context, parent);
	}

	@Override
	protected View newStandAloneView(Context context, ViewGroup parent) {
		return newChildView(context, parent);
	}
}
